package com.office.shop.by;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.office.shop.by.components.CAccounts;
import com.office.shop.by.components.CConfig;

/**
 * Created by IntelliJ IDEA.
 * User: zyr3x
 * Date: 19.04.11
 * Time: 13:52
 * To change this template use File | Settings | File Templates.
 */
public class AccountsActivity extends Activity {

    public static AccountsActivity obj;
    public ListView lvAccountList;
    public static int current_item_id;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.accountlist);
        obj = this;
        Refrash();
    }

    public void Refrash()
    {
        lvAccountList = (ListView)findViewById(R.id.lvAccountList);
        lvAccountList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , CAccounts.logins));
        lvAccountList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lvAccountList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
        public boolean onItemLongClick(AdapterView<?> arg0,
                                       View arg1,
                                       int arg2,
                                       long arg3) {
              current_item_id =   arg2;
              MenuItem();
              return false;
        }

    });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
           MenuInflater inflater = getMenuInflater();
           inflater.inflate(R.menu.account_menu, menu);
           return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.mmAdd:
                current_item_id = -1;
                  settingsActivity();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    private void settingsActivity()
    {
               Intent i = new Intent();
                 i.setClass(this, SettingsActivity.class);
                 i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 startActivity(i);
    }

    public int getId()
    {
        return current_item_id;
    }

    public void MenuItem()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setItems(new String[]{getString(R.string.Edit), getString(R.string.Delete)}, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {

                switch (item) {
                    case 0:
                        settingsActivity();
                        break;
                    case 1:
                        deleteItem();
                        break;
                }
                dialog.dismiss();

            }
        });
        AlertDialog alert = builder.create();
        alert.show();

    }

    public void deleteItem()
    {
         CAccounts.delete(current_item_id);
         Refrash();
    }

    public void addAccount(String login,String pwd)
    {
        if(login != "")
        {
           if(current_item_id == -1)
               CAccounts.add(login, pwd);
           else
               CAccounts.edit(current_item_id, login, pwd);
           Refrash();
        }
    }

    public void onBackPressed()
    {
        CAccounts.save(AccountsActivity.this);
        MultiAccActivity.obj.Refresh();
        finish();
    }
}